/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include "ScanMethodWMI.h"

//--------------------------------------------------------------------

ScanMethodWMI::ScanMethodWMI(void) : ScanMethod()
{
	myType = __wmtWMI;
}

ScanMethodWMI::~ScanMethodWMI(void)
{
}

//--------------------------------------------------------------------

int ScanMethodWMI::InternalInit(void)
{
	FillRandomBuffer();
	
	if(workPhase==__wptScan) return MSR_ERR_OK;

	return MSR_ERR_PHASE;
}

int ScanMethodWMI::InternalExecute(void)
{
	MMachineInfo* mmi;
	bool res;
	unsigned int scanVal;

	if(addrList==NULL)  return MSR_ERR_PARAMETER;
	if(wmiWorker==NULL) return MSR_ERR_PARAMETER;

	ScanPair sp;
	while(GetNextPair(&sp, __sptIpOnly) && !stopStopStop){
		if(sp.addr!=NULL){
			if(!sp.addr->addrIP.IsEmpty()){
				mmi = FindMachineByIp(sp.addr->addrIP.GetD());
				if(mmi!=NULL){
					mmi->wmiData.Initialize();

					scanVal = (unsigned int)_tstoi(scanParameters);
					if(scanVal!=0){
						if(config->xMinDelay>0){
							if(lastScanTime!=0){
								waitDone = false;
								while(!waitDone){
									CheckResponses();
									SleepEx(1, TRUE);
									scanTime = GetTickCount();
									scanTime = ComputeDeltaTime(lastScanTime, scanTime);
									if(scanTime>=config->xMinDelay)
										waitDone = true;
								}
							}
							lastScanTime = GetTickCount();
						}

						res = wmiWorker->ConnectToServer(sp.addr->addrIP.GetS(), _T("\\root\\cimv2"));
						if(res){
							if((mmi->wmiData.xStatus!=__wmisClosed) && (scanVal & __wmitW32CDROMDrive) && !stopStopStop){
								res = wmiWorker->ExecuteQueryInfo(&(mmi->wmiData.wmiCDROMDrive));
								mmi->wmiData.xStatus = res ? __wmisOpen : __wmisClosed;
								Notify();
							}
							if((mmi->wmiData.xStatus!=__wmisClosed) && (scanVal & __wmitW32DesktopMonitor) && !stopStopStop){
								res = wmiWorker->ExecuteQueryInfo(&(mmi->wmiData.wmiDesktopMonitor));
								mmi->wmiData.xStatus = res ? __wmisOpen : __wmisClosed;
								Notify();
							}
							if((mmi->wmiData.xStatus!=__wmisClosed) && (scanVal & __wmitW32DiskDrive) && !stopStopStop){
								res = wmiWorker->ExecuteQueryInfo(&(mmi->wmiData.wmiDiskDrive));
								mmi->wmiData.xStatus = res ? __wmisOpen : __wmisClosed;
								Notify();
							}
							if((mmi->wmiData.xStatus!=__wmisClosed) && (scanVal & __wmitW32DiskPartition) && !stopStopStop){
								res = wmiWorker->ExecuteQueryInfo(&(mmi->wmiData.wmiDiskPartition));
								mmi->wmiData.xStatus = res ? __wmisOpen : __wmisClosed;
								Notify();
							}

							if((mmi->wmiData.xStatus!=__wmisClosed) && (scanVal & __wmitW32Group) && !stopStopStop){
								res = wmiWorker->ExecuteQueryInfo(&(mmi->wmiData.wmiGroup));
								mmi->wmiData.xStatus = res ? __wmisOpen : __wmisClosed;
								Notify();
							}
							if((mmi->wmiData.xStatus!=__wmisClosed) && (scanVal & __wmitW32OperatingSystem) && !stopStopStop){
								res = wmiWorker->ExecuteQueryInfo(&(mmi->wmiData.wmiOperatingSystem));
								mmi->wmiData.xStatus = res ? __wmisOpen : __wmisClosed;
								Notify();
							}
							if((mmi->wmiData.xStatus!=__wmisClosed) && (scanVal & __wmitW32PhysicalMemory) && !stopStopStop){
								res = wmiWorker->ExecuteQueryInfo(&(mmi->wmiData.wmiPhysicalMemory));
								mmi->wmiData.xStatus = res ? __wmisOpen : __wmisClosed;
								Notify();
							}
							if((mmi->wmiData.xStatus!=__wmisClosed) && (scanVal & __wmitW32Processor) && !stopStopStop){
								res = wmiWorker->ExecuteQueryInfo(&(mmi->wmiData.wmiProcessor));
								mmi->wmiData.xStatus = res ? __wmisOpen : __wmisClosed;
								Notify();
							}

							if((mmi->wmiData.xStatus!=__wmisClosed) && (scanVal & __wmitW32QuickFixEngineering) && !stopStopStop){
								res = wmiWorker->ExecuteQueryInfo(&(mmi->wmiData.wmiQuickFixEngineering));
								mmi->wmiData.xStatus = res ? __wmisOpen : __wmisClosed;
								Notify();
							}
							if((mmi->wmiData.xStatus!=__wmisClosed) && (scanVal & __wmitW32Share) && !stopStopStop){
								res = wmiWorker->ExecuteQueryInfo(&(mmi->wmiData.wmiShare));
								mmi->wmiData.xStatus = res ? __wmisOpen : __wmisClosed;
								Notify();
							}
							if((mmi->wmiData.xStatus!=__wmisClosed) && (scanVal & __wmitW32UserAccount) && !stopStopStop){
								res = wmiWorker->ExecuteQueryInfo(&(mmi->wmiData.wmiUserAccount));
								mmi->wmiData.xStatus = res ? __wmisOpen : __wmisClosed;
								Notify();
							}
							if((mmi->wmiData.xStatus!=__wmisClosed) && (scanVal & __wmitW32VideoController) && !stopStopStop){
								res = wmiWorker->ExecuteQueryInfo(&(mmi->wmiData.wmiVideoController));
								mmi->wmiData.xStatus = res ? __wmisOpen : __wmisClosed;
								Notify();
							}
							if((mmi->wmiData.xStatus!=__wmisClosed) && (scanVal & __wmitW32LoggedUser) && !stopStopStop){
								res = wmiWorker->ExecuteQueryInfo(&(mmi->wmiData.wmiLoggedUser));
								mmi->wmiData.xStatus = res ? __wmisOpen : __wmisClosed;
								Notify();
							}
						}

						wmiWorker->DisconnectFromServer();
					}
				}
			}
		}
	}

	return MSR_ERR_OK;
}

//--------------------------------------------------------------------

void ScanMethodWMI::Notify(void)
{
	packSent++;
	if((packSent % notifNetBios) == 0 && packSent>0)
		if(hWnd!=NULL)
			PostMessage(hWnd, CHS_MSG_PACKETS_SENT, 0, (LPARAM)packSent);
}

//--------------------------------------------------------------------

void ScanMethodWMI::ComputeRandomValues(void)
{
	// nothing to do
}

//--------------------------------------------------------------------
